package com.ssm.start.util;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.FilterDef;
import org.apache.tomcat.util.descriptor.web.FilterMap;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class TomcatUtil {

    public static boolean isBlank(Map m){
        if(Objects.isNull(m) || m.size() ==0 ){
            return true;
        }
        return false ;
    }

    public static boolean isBlank(Collection c){
        if(Objects.isNull(c) || c.size() ==0 ){
            return true;
        }
        return false ;
    }


    /**
     *  启动tomcat
     * @param tomcat
     * @throws LifecycleException
     */
    public static void startTomcat( Tomcat tomcat ) throws LifecycleException {
        tomcat.start();
        tomcat.getServer().await();
    }

    public static void addContextParam(Context context,String k, String v){
        context.addParameter(k,v);
    }

    public static void addContextParam(Context context, Map<String,String> args){
        args.keySet().stream().forEach(k->addContextParam(context,k,args.get(k)));

    }

    public static void addListener(Context context,String listenerClassz){
        context.addApplicationListener(listenerClassz);

    }


    public static FilterDef addFilter(Context context , String filterClass, String filterName ,
                                 String description, List<String> urlPatten,Map<String,String> initParams)
    {
        FilterDef fd =new FilterDef();
        fd.setFilterName(filterName);
        fd.setFilterClass(filterClass);
        fd.setDescription(description);
        if(!isBlank(initParams)){
            initParams.keySet().stream().forEach(k->fd.addInitParameter(k,initParams.get(k)));
        }
        context.addFilterDef(fd);
        FilterMap fm  = null;
        if(!isBlank(urlPatten)){
            fm = new FilterMap();
            fm.setFilterName(filterName);
            for (String u : urlPatten){
                fm.addURLPattern(u);
            }
            context.addFilterMap(fm);
        }
        return fd;
    }
    public static FilterDef addFilter(Context context ,  FilterDef filterDef,FilterMap filterMap ){
        if(Objects.nonNull(filterDef)){
            context.addFilterDef(filterDef);
        }
        if(Objects.nonNull(filterMap)){
            context.addFilterMap(filterMap);
        }

        return filterDef;
    }

    public static Wrapper addServlet(Context context,String servletName,String servletClass,
            Map<String,String> initParams,List<String>urlPatten,int loadOnStartup){
        Wrapper sw = context.createWrapper();
        sw.setServletClass(servletClass);
        sw.setName(servletName);
        if(!isBlank(initParams)){
            initParams.keySet().stream().forEach(k->sw.addInitParameter(k,initParams.get(k)));
        }
        if(-1!=loadOnStartup){
            sw.setLoadOnStartup(loadOnStartup);
        }
        context.addChild(sw);
        if(!isBlank(urlPatten)){
            urlPatten.stream().forEach(u->context.addServletMappingDecoded(u,servletName));
        }
        return sw ;
    }
}
